<?php

/*
 * This will be the Security Group Setup page. 
 * This page involves :
 * Create Security Group Button
 * Security Group Search Filters
 * Security Group Search Results
 * 
 * Created by: Peter Agno Jr.
 * Date created: December 4, 2011
 */

    include("siteTop.php"); 
    include('includes/siteConfig.php'); 

    // connect to database
    $connect = mysql_connect($hostName,$rootName,$dBasePassword) or die ('Unable to connect!');
    mysql_select_db($dBaseName) or die ('Unable to select database!');
?>

<!-- HEADER START -->
    <!--JAVASCRIPT SOURCES-->
    <script src="js/jquery-1.6.2.min.js"></script>
    
    <!--ACTUAL JS SCRIPTS AND JQUERIES-->
    <script type="text/javascript">
        jQuery(document).ready(function(){   
            // CHANGE FIRST THE CSS OF THE NAV BAR
            jQuery("#adminTools-nav").addClass("highlightMenuBar");
            jQuery("#secGrpSetup-nav").addClass("highlightMenuBar");
            
            jQuery("#secGrpId").focus();
            dispSearchResults();    // Updates the search results of security groups onload
            
            // START - Ajax for search when enter button was clicked.
            jQuery(".inputbox").keypress(function(e){
               if(e.keyCode == 13) {
                    jQuery("#searchButton").click();
               }
            }); // END - Ajax for search when enter button was clicked.
            
            // Start - Call function that will search departments when search button was clicked
            jQuery('#searchButton').live("click",function() {
                dispSearchResults();
                jQuery("#secGrpId").focus();
            }); // End - Call function that will search departments when search button was clicked
            
            // Start - Function for searching security groups
            function dispSearchResults() {
                var varRow = "";
                
                // Start - Ajax the getting of security groups
                jQuery.ajax({
                    type : 'POST',
                    url : 'ajaxRequests/admSrchSecGrp.php',
                    dataType : 'json',
                    async : false,
                    data: {
                        secGrpId : jQuery("#secGrpId").val(),
                        secGrpName : jQuery("#secGrpName").val()
                    },
                    success : function(data){
                        // Remove the listed security groups in the table
                        jQuery("#searchResults").children().remove();
                        
                        // If, there are querried security groups
                        if (data) {
                            // Append the header
                            jQuery(".searchResults").append("<tr><th>Security Group ID</th><th>Security Group Name</th></tr>");
                        
                            // INCLUDE - SET THE VARIABLES FOR THE PAGINATION
                            <?php include("includes/jqueries/jQSetPaginationVar.php"); ?>
                        
                            // Start - Append the querried security groups
                            jQuery.each(data, function(index, data) {
                                // Start - Prepare the data
                                
                                // INCLUDE - SET THE VISIBILITY OF THE ROW FOR PAGINATION
                                <?php include("includes/jqueries/jQVisibilityRowPagination.php"); ?>
                                
                                varRow += "<td><a href='javascript:void(0)' class='hLinkViewPage' id='SecGrp__"+data.secGrpId+"'>"+data.secGrpId+"</a></td>";
//                                varRow += "<td><input type='hidden' id='secGrpId_"+data.secGrpId+"' value='"+data.secGrpId+"' />"+data.secGrpId+"</td>";
                                varRow += "<td><input type='hidden' id='secGrpName_"+data.secGrpId+"' value='"+data.secGrpName+"' />"+data.secGrpName+"</td>";
                                varRow += "</tr>";
                                // End - Prepare the data

                                jQuery(".searchResults").append(varRow);    // Append the data
                            }); // End - Append the querried security groups
                            
                            columnNum = "2"; // Set this for the colspan of the paginationNumBar
                            appendTo = "searchResults"; // Table on where it will be appended

                            // START - CREATING THE PAGINATION NUMBER BAR
                            // IF THERE IS ONLY ONE PAGE, THEN DON'T SHOW PAGINATION
                            if ( pageNum != 1 ) {
                                varRow = "<tr class='paginationNumBar'><td colspan='"+columnNum+"' align='center'><div class='paginationNumBarDiv'>";

                                // IF THE ACCUMULATED (OR TOTAL) $pageNum > $viewPageNumLimit THEN SHOW ONLY $viewPageNumLimit OF PAGE NUMBERS, AND DON'T SHOW FIRST 
                                if ( pageNum > viewPageNumLimit ) {
                                    varRow += "<input style='display:none;' class='paginationBtnCss paginationPageNumbers pageButton firstPageButton' id='rowPage_1' type='button' value='First' /> ";
                                    varRow += "<input style='display:none;' class='paginationBtnCss paginationPageNumbers pageButton prevPageButton' id='rowPage_1' type='button' value='Previous' /> ";

                                    for ( i = 1 ; i <= pageNum ; i++ ) {
                                        if ( i <= viewPageNumLimit ) {
                                            if ( firstRow == true ) {
                                                firstRow = false;
                                                varRow += "<input class='paginationCurrentPage ";
                                            }
                                            else {
                                                varRow += "<input class='paginationPageNumbers ";
                                            }
                                        }
                                        else {
                                            varRow += "<input style='display:none;' class='paginationPageNumbers ";
                                        }

                                        varRow += "paginationBtnCss pageButton buttonNum_"+i+"' id='rowPage_"+i+"' type='button' value='"+i+"' /> ";
                                    }

                                    varRow += "<input class='paginationBtnCss paginationPageNumbers pageButton nextPageButton' id='rowPage_2' type='button' value='Next' /> ";
                                    varRow += "<input class='paginationBtnCss paginationPageNumbers pageButton lastPageButton' id='rowPage_"+pageNum+"' type='button' value='Last' /> ";
                                }
                                else {
                                // ELSE, SHOW ALL PAGE NUMBERS
                                    for ( i = 1 ; i <= pageNum ; i++ ) {                    
                                        if ( firstRow == true ) {
                                            firstRow = false;
                                            varRow += "<input class='paginationCurrentPage ";
                                        }
                                        else {
                                            varRow += "<input class='paginationPageNumbers ";
                                        }

                                        varRow += "paginationBtnCss pageButton buttonNum_"+i+"' id='rowPage_"+i+"' type='button' value='"+i+"' /> ";
                                    }
                                }

                                varRow += "</div></td></tr>";
                                jQuery("."+appendTo).append(varRow);
                            }

                            jQuery("#totalPageNum").val(pageNum);
                        // END - CREATING THE PAGINATION NUMBER BAR

                        } // End if, there are querried security groups
                        else {
                        // No data returned from ajax, so make it empty
                            varRow = "<tr><td colspan='2'><span class='errorMessage'>No security groups were found.</span></td></tr>";
                            jQuery(".searchResults").append(varRow);
                        }
                    },
                    error : function() {
                        alert("Error : Getting the security groups was not successful.");
                    }
                }); // End - Ajax the getting of security groups
            } // End - Function for searching 
            
            // START - JQUERY FOR PAGINATION
            jQuery('.pageButton').live("click",function() {
                buttonId = this.id;

                tempSubstr = buttonId.split('_');
                totalPageNum = jQuery("#totalPageNum").val();
                viewPageNumLimit = jQuery("#viewPageNumLimit").val();

                // HIDE ALL ROWS IN THE SEARCH RESULTS ASCENDING
                for ( i = 1 ; i < tempSubstr[1] ; i++ ) {
                    jQuery(".rowPage_"+i).hide();
                }

                // HIDE ALL ROWS IN THE SEARCH RESULTS DESCENDING
                for ( i = totalPageNum ; i > tempSubstr[1] ; i-- ) {
                    jQuery(".rowPage_"+i).hide();
                }

                // SHOW THE SELECTED ROWS
                jQuery("."+buttonId).show();

                // SHOW ONLY LIMITED NUMBERS OF PAGE, DEPENDING ON THE viewPageNumLimit
                if ( totalPageNum > viewPageNumLimit ) {
                    // DECREMENT AND SET THE prevPageButton
                    prevPage = parseInt(parseFloat(tempSubstr[1])) - 1;
                    jQuery(".prevPageButton").attr("id", "rowPage_"+prevPage);

                    // INCREMENT AND SET THE nextPageButton
                    nextPage = parseInt(parseFloat(tempSubstr[1])) + 1;
                    jQuery(".nextPageButton").attr("id", "rowPage_"+nextPage);

                    // HIDE ALL PAGINATION BUTTONS
                    jQuery(".pageButton").hide();

                    // SHOW PAGINATION NUMBERS WITHIN viewPageNumLimit
                    for ( i = parseInt( tempSubstr[1] ) - ( parseInt( viewPageNumLimit / 2 ) ), viewCount = 1 ; i <= totalPageNum && viewCount <= viewPageNumLimit ; i++, viewCount++ ) {
                        jQuery(".buttonNum_"+i).show();
                    }

                    // SHOW SPECIAL PAGINATION BUTTONS
                    jQuery(".firstPageButton").show();
                    jQuery(".prevPageButton").show();
                    jQuery(".nextPageButton").show();
                    jQuery(".lastPageButton").show();

                    // HIDE nextPageButton AND lastPageButton IF THE LAST PAGE OR FINAL NUMBER WAS CLICKED
                    if ( tempSubstr[1] == totalPageNum ) {
                        jQuery(".nextPageButton").hide();
                        jQuery(".lastPageButton").hide();
                    }

                    // HIDE firstPageButton AND prevPageButton IF THE FIRST PAGE OR PAGE 1 WAS CLICKED
                    if ( tempSubstr[1] == 1 ) {           
                        jQuery(".prevPageButton").hide();
                        jQuery(".firstPageButton").hide();
                    }


                }

                // UPDATE THE PREVIOUS PAGE BUTTON
                // jQuery(".pageButton").css({ 'color': 'blue' });
                jQuery(".pageButton").removeClass("paginationCurrentPage").addClass("paginationPageNumbers");

                // COLOR THE CLICKED PAGINATION NUMBER
                // jQuery(".buttonNum_"+tempSubstr[1]).css({ 'color': 'red' });
                jQuery(".buttonNum_"+tempSubstr[1]).removeClass("paginationPageNumbers").addClass("paginationCurrentPage");
            }); // END - JQUERY FOR PAGINATION
            
            // ############################## Start - Add includes here ##############################
            <?php             
                // INCLUDE - JQUERY FOR TRANSFERRING TO VIEW PAGE OF THE SECURITY GROUP
                include("includes/jqueries/jQViewPageTransfer.php"); 
            ?>
            // ############################## End - Add includes here ##############################
            
        });
    </script>
<!-- HEAD END -->

<?php include("siteMiddle.php"); ?>

<div id="content">
<h1>Security Group List</h1>
    <div class="contentbox">       
        <?php
        // Check permission
        $thePermissions = array('SECGRPSTP');
        if ( checkPermission( $thePermissions, $userId ) == true ) {
        ?>
            <div class="messageBox successMessage successMessage_fromBase"
                <?php
                // Handles the success messages from the create, edit, delete of department.
                if(isset($_POST['secGrpSuccessMessage'])) {
                    echo ">";
                    echo "<div id='successMessages' class='successMessages_fromBase'>".$_POST['secGrpSuccessMessage']."</div>";
                }
                else {
                    echo "style='display:none;'>";
                    echo "<div id='successMessages' class='successMessages_fromBase'></div>";
                }
                ?>
            </div>
        
            <form id="createSecGrpForm" name="createSecGrpForm" action="admSecGroupCreate.php" method="post">
                <input type="Submit" name="createSecGrpBtn" id="createSecGrpBtn" value="Create Security Group" class="siteGreenButton" />
            </form>
            <br/>
            
            <!-- START - CREATE SEARCH FILTERS -->
            <span class="screenlet" >Search Filters</span>
            <table>
                <tr>
                    <td>Security Group ID : </td>
                    <td>
                        <input type="text" value="" name="secGrpId" id="secGrpId" class="inputbox" />
                    </td>
                </tr>
                <tr>
                    <td>Security Group Name : </td>
                    <td>
                        <input type="text" value="" name="secGrpName" id="secGrpName" class="inputbox" />
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="Submit" name="searchButton" id="searchButton" value="Search" class="siteGreenButton" /></td>
                </tr>
            </table>
            <!-- END - CREATE SEARCH FILTERS -->
            
            <!-- START - CREATE SEARCH RESULTS -->
            <span class="screenlet" >Search Results</span>
            <table border="1" name="searchResults" id="searchResults" class="searchResults">
                <?php
                    // INCLUDE - SET THE VARIABLES FOR THE PAGINATION
                    include('includes/variables/setPaginationVariables.php');
                ?>
            </table>
            <?php 
                // INCLUDE - HIDDEN VARIABLES USED FOR PAGINATION
                include('includes/variables/hiddenVarPagination.php'); 
            ?>
            <!-- END - CREATE SEARCH RESULTS -->
        <?php
        }
        else {
            echo "<div class='noPermissionMsgBox noPermissionMsg' style='border: 2px solid red'><div id='noPermissionMsgs'>You have no permission to view this page.</div></div>";
        }
        // End checking permission
        ?>
    </div> <!-- contentbox end div -->
    <div style="position:relative; visibility: hidden">end</div>    <!-- extra space for contentbox -->
</div> <!-- content end div -->

<!-- INVISIBLE FORM WHICH WILL BE USED FOR TRANSFERRING THE SECURITY GROUP TO ITS VIEW PAGE -->
<form id="viewPageForm" name="viewPageForm" action="admSecGroupView.php" method="post">
    <input id="passedId" name="securityGroupId" type="hidden" value="" />
</form>


<?php
    include ("siteBottom.php"); 
?>